import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class Client {
	static String resp = "";
	static {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < 1024; i++) {
			sb.append(
					"1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890");
		}
		resp += sb.toString() + "\r\n";
	}

	public static void main(String[] args) throws Exception {
		long TM = System.currentTimeMillis();
		// for (int i = 0; i < 1000; i++) {
		Socket c = new Socket("localhost", 28888);
		proc(c);
		// }
		TM = System.currentTimeMillis() - TM;
		System.out.println(TM);
	}

	static void proc(Socket c) throws IOException {
		final BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));

		new Thread() {
			public void run() {
				try {
					read(br);
				} catch (IOException e) {
					e.printStackTrace();
				}
			};
		}.start();

		final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(c.getOutputStream()));

		new Thread() {
			public void run() {
				try {
					write(bw);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

		}.start();
	}

	private static void write(BufferedWriter bw) throws IOException {
		System.out.println("start client write");
		for (int i = 0; i < 1000; i++) {
			bw.write(resp);
			bw.flush();
		}
		bw.close();
	}

	private static void read(final BufferedReader br) throws IOException {
		System.out.println("start client read");
		int count = 0;
		String line;
		while ((line = br.readLine()) != null) {
			count += 1;
			// if(count%1024==0)
			// System.out.println(count/1024);
			System.out.println(count);
		}
		br.close();
	}
}
